﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.UI.Views;
using StockFinder.Services.Extractors;
using StockFinder.Services.Extractors.Yahoo;
using StockFinder.DataAccess;
using StockFinder.Model;
using StockFinder.Model.Enumerations;
using System.Threading;
using StockFinder.Services.Engines.Yahoo;
using StockFinder.Services.Engines;

namespace StockFinder.UI.Controllers
{
    public class DownloadAllHistoricPricesViewController
    {
        private readonly IDownloadAllHistoricPricesView _View;
        private readonly IHistoricPricesExtractor _HistoricPricesExtractor;
        private readonly SymbolDataAccess _SymbolDataAccess;
        private readonly DailyPriceDataAccess _DailyPriceDataAccess;
        private readonly IDownloadHistoricPricesAllSymbolsEngine _DownloadHistoricPricesAllSymbolsEngine;

        public DownloadAllHistoricPricesViewController(IDownloadAllHistoricPricesView view)
        {
            _View = view;

            _HistoricPricesExtractor = new YahooHistoricPriceExtractor();
            _SymbolDataAccess = new SymbolDataAccess();
            _DailyPriceDataAccess = new DailyPriceDataAccess();
            _DownloadHistoricPricesAllSymbolsEngine = new YahooDownloadHistoricPricesAllSymbolsEngine();

            //set event handlers
            _DownloadHistoricPricesAllSymbolsEngine.LogMessage += new Action<string>(_DownloadHistoricPricesAllSymbolsEngine_LogMessage);
            _DownloadHistoricPricesAllSymbolsEngine.UpdateDiagnostics += new Action<YahooDownloadHistoricPricesDiagnostics>(_DownloadHistoricPricesAllSymbolsEngine_UpdateDiagnostics);
            _View.Start += new EventHandler(_View_Start);
            _View.Stop += new EventHandler(_View_Stop);
        }

        void _DownloadHistoricPricesAllSymbolsEngine_UpdateDiagnostics(YahooDownloadHistoricPricesDiagnostics diagnostics)
        {
            _View.SetAverageSecondsTaken(diagnostics.AverageSecondsTaken);

            _View.UpdateEstimatedTimeRemaining(diagnostics.TimeRemaining);

            _View.UpdateProgress(diagnostics.ProgressMinimum, diagnostics.ProgressMaximum, diagnostics.ProgressValue);
        }

        void _DownloadHistoricPricesAllSymbolsEngine_LogMessage(string message)
        {
            _View.AppendLog(message);
        }

        void _View_Stop(object sender, EventArgs e)
        {
            if (_DownloadHistoricPricesAllSymbolsEngine != null)
            {
                _DownloadHistoricPricesAllSymbolsEngine.Stop();
            }
        }

        void _View_Start(object sender, EventArgs e)
        {
            //invoke this in separate thread to stop UI from hanging
            Thread thread = new Thread(new ThreadStart(delegate
            {
                //set date parameters
                _DownloadHistoricPricesAllSymbolsEngine.DateFrom = _View.GetDateFrom();
                _DownloadHistoricPricesAllSymbolsEngine.DateTo = _View.GetToDate();

                //start the thread
                _DownloadHistoricPricesAllSymbolsEngine.Start();
            }));

            thread.Name = "DownloadAllHistoricPricesThread";
            thread.Start();
        }
    }
}
